package de.fhdw.hbs.client.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.xml.datatype.DatatypeConfigurationException;

import org.jdesktop.swingx.JXTaskPaneContainer;

import com.googlecode.starrating.StarRating;
import com.toedter.calendar.JDateChooser;

import de.fhdw.hbs.client.generated.HbsServiceException_Exception;
import de.fhdw.hbs.client.generated.Hotel;
import de.fhdw.hbs.client.generated.HotelListDTO;
import de.fhdw.hbs.client.generated.HotelSucheDTO;
import de.fhdw.hbs.client.generated.KundeDTO;
import de.fhdw.hbs.client.generated.LoginDTO;
import de.fhdw.hbs.client.ws.HbsClient;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * 
 * @author kuerp01
 *
 */
public class GUI {

	private JFrame frame;
	private JPanel panelSuchen;
	
	
	//Web Service calls
	HbsClient wsClient = new HbsClient();
	private JTextField txtName;
	private JTextField txtLand;
	private JTextField txtOrt;
	private JTextField txtAnzahlZimmer;
	private JTextField txtPreisVon;
	private JTextField txtPreisBis;
	
	private LoginDTO login;
	private int kundenId;

	/**
	 * Create the application.
	 */
	public GUI() {
		initialize();
	}
	public GUI(LoginDTO login) {
		this.login = login;
		this.kundenId = login.getUserId();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.getContentPane().setBackground(UIManager.getColor("InternalFrame.inactiveTitleGradient"));
		frame.getContentPane().setForeground(Color.BLACK);
		frame.setBounds(100, 100, 720, 688);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		SpringLayout springLayout = new SpringLayout();
		frame.getContentPane().setLayout(springLayout);
		frame.setTitle("FHDW HotelBookingService Client");
		
		panelSuchen = new JPanel();
		springLayout.putConstraint(SpringLayout.NORTH, panelSuchen, 10, SpringLayout.NORTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, panelSuchen, 10, SpringLayout.WEST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, panelSuchen, 650, SpringLayout.NORTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, panelSuchen, 694, SpringLayout.WEST, frame.getContentPane());
		panelSuchen.setBackground(UIManager.getColor("InternalFrame.inactiveTitleGradient"));
		
		frame.getContentPane().add(panelSuchen);
		
		SpringLayout sl_panelSuchen = new SpringLayout();
		panelSuchen.setLayout(sl_panelSuchen);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setFont(new Font("Calibri", Font.BOLD, 13));
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblName, 10, SpringLayout.NORTH, panelSuchen);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, lblName, 10, SpringLayout.WEST, panelSuchen);
		panelSuchen.add(lblName);
		
		JLabel lblSuchergebnisse = new JLabel("Suchergebnisse");
		sl_panelSuchen.putConstraint(SpringLayout.WEST, lblSuchergebnisse, 0, SpringLayout.WEST, lblName);
		lblSuchergebnisse.setFont(new Font("Arial", Font.BOLD, 12));
		panelSuchen.add(lblSuchergebnisse);
		
		final JPanel jpErgebnisse = new JPanel();
		jpErgebnisse.setBorder(new LineBorder(new Color(255, 140, 0), 3));
				
		JScrollPane scrollPane = new JScrollPane();
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, scrollPane, 6, SpringLayout.SOUTH, lblSuchergebnisse);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, scrollPane, 10, SpringLayout.WEST, panelSuchen);
		sl_panelSuchen.putConstraint(SpringLayout.SOUTH, scrollPane, -10, SpringLayout.SOUTH, panelSuchen);
		sl_panelSuchen.putConstraint(SpringLayout.EAST, scrollPane, 674, SpringLayout.WEST, panelSuchen);
		scrollPane.setViewportView(jpErgebnisse);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		jpErgebnisse.setBounds(scrollPane.getBounds());
		jpErgebnisse.setLayout(null);
		panelSuchen.add(scrollPane);
		
		JLabel lblOrt = new JLabel("Ort:");
		sl_panelSuchen.putConstraint(SpringLayout.EAST, lblOrt, -352, SpringLayout.EAST, panelSuchen);
		lblOrt.setFont(new Font("SansSerif", Font.BOLD, 12));
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblOrt, -1, SpringLayout.NORTH, lblName);
		panelSuchen.add(lblOrt);
		
		JLabel lblLand = new JLabel("Land:");
		lblLand.setFont(new Font("SansSerif", Font.BOLD, 12));
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblLand, 16, SpringLayout.SOUTH, lblName);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, lblLand, 0, SpringLayout.WEST, lblName);
		panelSuchen.add(lblLand);
		
		JLabel lblHotelKategorie = new JLabel("Hotel Kategorie");
		lblHotelKategorie.setFont(new Font("SansSerif", Font.BOLD, 12));
		sl_panelSuchen.putConstraint(SpringLayout.WEST, lblHotelKategorie, 0, SpringLayout.WEST, lblName);
		panelSuchen.add(lblHotelKategorie);
		
		JLabel lblZimmerKategorie = new JLabel("Zimmer Kategorie");
		lblZimmerKategorie.setFont(new Font("SansSerif", Font.BOLD, 12));
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblZimmerKategorie, 109, SpringLayout.NORTH, panelSuchen);
		sl_panelSuchen.putConstraint(SpringLayout.SOUTH, lblHotelKategorie, -11, SpringLayout.NORTH, lblZimmerKategorie);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, lblZimmerKategorie, 10, SpringLayout.WEST, panelSuchen);
		panelSuchen.add(lblZimmerKategorie);
		
		txtName = new JTextField();
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, txtName, -2, SpringLayout.NORTH, lblName);
		sl_panelSuchen.putConstraint(SpringLayout.EAST, txtName, -70, SpringLayout.WEST, lblOrt);
		panelSuchen.add(txtName);
		txtName.setColumns(10);
		
		txtLand = new JTextField();
		sl_panelSuchen.putConstraint(SpringLayout.WEST, txtLand, 43, SpringLayout.EAST, lblLand);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, txtName, 0, SpringLayout.WEST, txtLand);
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, txtLand, -1, SpringLayout.NORTH, lblLand);
		txtLand.setColumns(10);
		panelSuchen.add(txtLand);
		
		txtOrt = new JTextField();
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, txtOrt, -2, SpringLayout.NORTH, lblName);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, txtOrt, 5, SpringLayout.EAST, lblOrt);
		txtOrt.setColumns(10);
		panelSuchen.add(txtOrt);
		
		JLabel lblAnzahlZimmer = new JLabel("Anzahl Zimmer:");
		sl_panelSuchen.putConstraint(SpringLayout.EAST, txtLand, -70, SpringLayout.WEST, lblAnzahlZimmer);
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblAnzahlZimmer, 0, SpringLayout.NORTH, lblLand);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, lblAnzahlZimmer, 0, SpringLayout.WEST, lblOrt);
		lblAnzahlZimmer.setFont(new Font("SansSerif", Font.BOLD, 12));
		panelSuchen.add(lblAnzahlZimmer);
		
		txtAnzahlZimmer = new JTextField();
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, txtAnzahlZimmer, -1, SpringLayout.NORTH, lblLand);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, txtAnzahlZimmer, 6, SpringLayout.EAST, lblAnzahlZimmer);
		sl_panelSuchen.putConstraint(SpringLayout.EAST, txtAnzahlZimmer, -242, SpringLayout.EAST, panelSuchen);
		txtAnzahlZimmer.setColumns(10);
		panelSuchen.add(txtAnzahlZimmer);
		
		final StarRating starHotel = new StarRating();
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, starHotel, 0, SpringLayout.NORTH, lblHotelKategorie);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, starHotel, 32, SpringLayout.EAST, lblHotelKategorie);
		panelSuchen.add(starHotel);
		
		final StarRating starZimmer = new StarRating();
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, starZimmer, 0, SpringLayout.NORTH, lblZimmerKategorie);
		sl_panelSuchen.putConstraint(SpringLayout.EAST, starZimmer, 0, SpringLayout.EAST, starHotel);
		panelSuchen.add(starZimmer);
		
		final StarRating starBewertung = new StarRating();
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, starBewertung, 6, SpringLayout.SOUTH, starZimmer);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, starBewertung, 0, SpringLayout.WEST, starHotel);
		panelSuchen.add(starBewertung);
		
		JLabel lblBewertung = new JLabel("Bewertung");
		lblBewertung.setFont(new Font("SansSerif", Font.BOLD, 12));
		sl_panelSuchen.putConstraint(SpringLayout.WEST, lblBewertung, 0, SpringLayout.WEST, lblName);
		sl_panelSuchen.putConstraint(SpringLayout.SOUTH, lblBewertung, 0, SpringLayout.SOUTH, starBewertung);
		panelSuchen.add(lblBewertung);
		
		JLabel lblZeitraum = new JLabel("Zeitraum");
		lblZeitraum.setFont(new Font("SansSerif", Font.BOLD, 12));
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblZeitraum, 0, SpringLayout.NORTH, lblHotelKategorie);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, lblZeitraum, 0, SpringLayout.WEST, lblOrt);
		panelSuchen.add(lblZeitraum);
		
		JLabel lblDateVon = new JLabel("von:");
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblDateVon, 0, SpringLayout.NORTH, lblZimmerKategorie);
		sl_panelSuchen.putConstraint(SpringLayout.EAST, lblDateVon, 0, SpringLayout.EAST, lblZeitraum);
		panelSuchen.add(lblDateVon);
		
		JLabel lblDateBis = new JLabel("bis:");
		sl_panelSuchen.putConstraint(SpringLayout.SOUTH, lblDateBis, 0, SpringLayout.SOUTH, starBewertung);
		sl_panelSuchen.putConstraint(SpringLayout.EAST, lblDateBis, 0, SpringLayout.EAST, lblZeitraum);
		panelSuchen.add(lblDateBis);
		
		final JDateChooser dateVon = new JDateChooser();
		sl_panelSuchen.putConstraint(SpringLayout.WEST, dateVon, 6, SpringLayout.EAST, lblDateVon);
		sl_panelSuchen.putConstraint(SpringLayout.SOUTH, dateVon, 0, SpringLayout.SOUTH, starZimmer);
		panelSuchen.add(dateVon);

		final JDateChooser dateBis = new JDateChooser();
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, dateBis, 0, SpringLayout.NORTH, starBewertung);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, dateBis, 6, SpringLayout.EAST, lblDateBis);
		panelSuchen.add(dateBis);
		
		JLabel lblPreis = new JLabel("Preis");
		lblPreis.setFont(new Font("SansSerif", Font.BOLD, 12));
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblPreis, 16, SpringLayout.SOUTH, lblBewertung);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, lblPreis, 10, SpringLayout.WEST, panelSuchen);
		panelSuchen.add(lblPreis);
		
		JLabel lblPreisVon = new JLabel("von:");
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblPreisVon, 6, SpringLayout.SOUTH, lblPreis);
		sl_panelSuchen.putConstraint(SpringLayout.EAST, lblPreisVon, 0, SpringLayout.EAST, lblName);
		panelSuchen.add(lblPreisVon);
		
		JLabel lblPreisBis = new JLabel("bis:");
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblPreisBis, 0, SpringLayout.NORTH, lblPreisVon);
		panelSuchen.add(lblPreisBis);
		
		txtPreisVon = new JTextField();
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, lblSuchergebnisse, 45, SpringLayout.SOUTH, txtPreisVon);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, lblPreisBis, 6, SpringLayout.EAST, txtPreisVon);
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, txtPreisVon, -6, SpringLayout.NORTH, lblPreisVon);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, txtPreisVon, 6, SpringLayout.EAST, lblPreisVon);
		sl_panelSuchen.putConstraint(SpringLayout.EAST, txtPreisVon, 52, SpringLayout.EAST, lblPreisVon);
		panelSuchen.add(txtPreisVon);
		txtPreisVon.setColumns(10);
		
		txtPreisBis = new JTextField();
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, txtPreisBis, -6, SpringLayout.NORTH, lblPreisVon);
		sl_panelSuchen.putConstraint(SpringLayout.WEST, txtPreisBis, 6, SpringLayout.EAST, lblPreisBis);
		sl_panelSuchen.putConstraint(SpringLayout.EAST, txtPreisBis, -507, SpringLayout.EAST, panelSuchen);
		txtPreisBis.setColumns(10);
		panelSuchen.add(txtPreisBis);
		
		JButton btnSuchen = new JButton("Suchen");
		sl_panelSuchen.putConstraint(SpringLayout.WEST, btnSuchen, 0, SpringLayout.WEST, lblName);
		sl_panelSuchen.putConstraint(SpringLayout.SOUTH, btnSuchen, -6, SpringLayout.NORTH, lblSuchergebnisse);
		
		btnSuchen.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				HotelSucheDTO kriterien = new HotelSucheDTO();
				
				//Text fields
				kriterien.setName(txtName.getText());
				kriterien.setOrt(txtOrt.getText());
				kriterien.setLand(txtLand.getText());
				
				//Star Rating
				kriterien.setHotelKategorie((int) starHotel.getRate());
				kriterien.setZimmerKategorie((int) starZimmer.getRate());
				kriterien.setBewertung((int) starBewertung.getRate());
				
				//Parse Integer fields unless they are empty
				if ( ! txtPreisVon.getText().equals("") )     { kriterien.setPreisVon(Integer.parseInt(txtPreisVon.getText())); }
				if ( ! txtPreisBis.getText().equals("") )     { kriterien.setPreisBis(Integer.parseInt(txtPreisBis.getText())); }
				if ( ! txtAnzahlZimmer.getText().equals("") ) { kriterien.setAnzahlZimmer(Integer.parseInt(txtAnzahlZimmer.getText())); }
				
				//Date fields
				try {
					kriterien.setVon(de.fhdw.hbs.client.utils.Mapper.mapCalendar2XML(dateVon.getCalendar()));
				} catch (DatatypeConfigurationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					kriterien.setBis(de.fhdw.hbs.client.utils.Mapper.mapCalendar2XML(dateBis.getCalendar()));
				} catch (DatatypeConfigurationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				HotelListDTO hotels = new HotelListDTO();
				//Call web service
				try {
					hotels = wsClient.getHotelByKriterien(kriterien);
				} catch (HbsServiceException_Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				JXTaskPaneContainer tpContainer = new JXTaskPaneContainer();
				tpContainer.setBounds(jpErgebnisse.getBounds());
				jpErgebnisse.removeAll();
				//Clear old taskPanes within jpErgebnisse
				jpErgebnisse.add(tpContainer);
				
				//tpContainer.setLayout(new GridLayout(hotels.getList().size(),1));
				tpContainer.setLayout(new FlowLayout());
				tpContainer.setBounds(jpErgebnisse.getBounds());
				
				//Build TaskPanes
				for (Hotel hotel : hotels.getList()) {
					HotelPane hp = new HotelPane(frame, hotel, kundenId);
					hp.setPreferredSize(new Dimension(jpErgebnisse.getWidth(),jpErgebnisse.getHeight()/10));
					tpContainer.add(hp);
					hp.setCollapsed(true);
					
					//Add a listener to collapse all other hotelPanes if one is selected
					hp.addPropertyChangeListener(new CollapseListener(tpContainer, hp));
				}
				jpErgebnisse.setVisible(true);
			}
		});
		panelSuchen.add(btnSuchen);
		
		JButton btnReservierungenAnsehen = new JButton("Reservierungen ansehen");
		btnReservierungenAnsehen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ReservierungensAnzeigeDialog rad = new ReservierungensAnzeigeDialog(frame, login.getUserId());
			}
		});
		sl_panelSuchen.putConstraint(SpringLayout.NORTH, btnReservierungenAnsehen, 0, SpringLayout.NORTH, lblName);
		sl_panelSuchen.putConstraint(SpringLayout.SOUTH, btnReservierungenAnsehen, 0, SpringLayout.SOUTH, lblLand);
		sl_panelSuchen.putConstraint(SpringLayout.EAST, btnReservierungenAnsehen, -10, SpringLayout.EAST, panelSuchen);
		panelSuchen.add(btnReservierungenAnsehen);
		
		this.frame.setResizable(false);
	}
	
	public int getKundenId() {
		return kundenId;
	}

	public JFrame getFrame() {
		return frame;
	}

	public Color getFrameContentPaneBackground() {
		return panelSuchen.getBackground();
	}
	public void setFrameContentPaneBackground(Color background) {
		panelSuchen.setBackground(background);
	}
}
